package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageBean getList(Integer page, Integer pageSize) {
//        Long total = empMapper.count();
//        Integer startIndex = (page - 1) * pageSize;
//        List<Emp> list = empMapper.list(startIndex,pageSize);


        /*
         * PageHelper：分页插件
         * getTotal()：获得总页数
         * getResult()：获得分页后数据列表
         * */
        PageHelper.startPage(page, pageSize);
        Page<Emp> list = empMapper.list();

        return new PageBean(list.getTotal(), list.getResult());
    }

    @Override
    public void save(Emp emp) {
        //1.补全基础属性
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //2.保存员工基本信息
        empMapper.insert(emp);

        //3. 保存员工的工作经历信息 - 批量
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        Emp emp = empMapper.getInfo(id);
        return emp;
    }

    @Transactional
    @Override
    public void update(Emp emp) {
        //修改员工信息
        empMapper.updateById(emp);

        //修改员工工作经历信息  ----> 先删除，再新增
        //1.先删除
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //2.再新增
        if(!CollectionUtils.isEmpty(emp.getExprList())){
            empExprMapper.insertBatch(emp.getExprList());
        }
    }

    @Override
    public List<Emp> getTeacherList() {
        List<Emp> list = empMapper.getTeacherList();
        return list;
    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if(empLogin != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("id", empLogin.getId());
            dataMap.put("username", empLogin.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}
